package com.southwind.service.impl;

import com.southwind.dto.AccountDto;
import com.southwind.entity.DormitoryAdmin;
import com.southwind.entity.SystemAdmin;
import com.southwind.form.AccountForm;
import com.southwind.mapper.DormitoryAdminMapper;
import com.southwind.mapper.SystemAdminMapper;
import com.southwind.service.AccountService;
import com.southwind.service.DormitoryAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private SystemAdminMapper systemAdminMapper;
    @Autowired
    private DormitoryAdminMapper dormitoryAdminMapper;

    @Override
    public AccountDto login(AccountForm accountForm) {
        AccountDto dto =new AccountDto();
        //判断类型
        switch (accountForm.getType())
        {
            case "systemAdmin":     //系统管理员
                SystemAdmin systemAdmin = this.systemAdminMapper.findByUsername(accountForm.getUsername());  //查询是否存在
                //判断用户名是否正确
                if(systemAdmin == null)
                {
                    dto.setCode(-1);
                }else {
                  //密码是否正确
                    if(!systemAdmin.getPassword().equals(accountForm.getPassword()))
                    {
                        dto.setCode(-2);
                    }
                    else {
                        dto.setCode(0);
                        dto.setAdmin(systemAdmin);
                    }
                }
                break;
            case "dormitoryAdmin":  //宿管
                DormitoryAdmin byUserName = this.dormitoryAdminMapper.findByUserName(accountForm.getUsername());
                if(byUserName == null){
                    dto.setCode(-1);
                }else {
                    if(!byUserName.getPassword().equals(accountForm.getPassword()))
                    {
                        dto.setCode(-2);
                    }else {
                        dto.setCode(0);
                        dto.setAdmin(byUserName);
                    }
                }
                break;
        }
        return dto;
    }
}
